package cd.tools;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

public class NetworkServer extends Service{
	
	private NetworkListener mNetworkListener;
	public void startCheckNetworkStatus(){
		Timer timer = new Timer();
		TimerTask timerTask = new TimerTask(){
			@Override
			public void run() {
				if(NetworkUtil.isNetworkAvailable(NetworkServer.this)){
					mNetworkListener.networkAvailable();
				}else{
					mNetworkListener.networkNotAvailable();
				}
			}			
		};
		timer.schedule(timerTask,0,2000*60);//延时0s，每隔2分钟执行一次run方法
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return new NetworkBinder();
	}

	public class NetworkBinder extends Binder{
		public NetworkServer getService(){
			return NetworkServer.this;
		}
	}
	
	public void setNetworkListener(NetworkListener mNetworkListener){
		this.mNetworkListener=mNetworkListener;
	}
	public interface NetworkListener{
		public void networkAvailable();
		public void networkNotAvailable();
	}
}
